#!/usr/bin/env python
#coding=utf-8

import os
import sys
import subprocess

# Add the current directory to Python path
sys.path.append(os.path.dirname(os.path.realpath(__file__)))

def main():
    """Start the FastAPI server"""
    print("Starting OHOS ArchInfo FastAPI Server...")

    # Check if all required modules are available
    try:
        import uvicorn
        import fastapi
        print("✓ Required dependencies are available")
    except ImportError as e:
        print(f"❌ Missing dependency: {e}")
        print("Please install required packages:")
        print("pip install fastapi uvicorn")
        sys.exit(1)

    # Import the app
    try:
        from main import app
        import config
        print(f"✓ FastAPI app loaded successfully")
    except Exception as e:
        print(f"❌ Failed to load FastAPI app: {e}")
        sys.exit(1)

    print(f"🚀 Starting server on {config.SERVER_IP}:{config.SERVER_PORT}")
    print("📖 API Documentation: http://127.0.0.1:9999/docs")
    print("🌐 Frontend URL: http://127.0.0.1:9999")
    print("⏹️  Press Ctrl+C to stop the server")
    print("-" * 50)

    # Start the server
    try:
        import uvicorn
        uvicorn.run(
            app,
            host=config.SERVER_IP,
            port=config.SERVER_PORT,
            reload=False,
            log_level="info"
        )
    except KeyboardInterrupt:
        print("\n👋 Server stopped by user")
    except Exception as e:
        print(f"❌ Server error: {e}")
        sys.exit(1)

if __name__ == "__main__":
    main()